package com.iter.devbox.javabean;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

public class JavaBeanApp {
	
	/**
     * 获取传入JavaBean的所有属性，包含其父类属性
     * 
     * @throws Exception
     */
	//@Test
	public void test1 () throws IntrospectionException {
		//传入要内省的Class对象，获取BeanInfo
        BeanInfo beanInfo = Introspector.getBeanInfo(Student.class);
        
        //通过BeanInfo获取属性描述器数组
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        
        //获取Student所有的属性(继承父类属性：class, luckyNum, name)
        for (PropertyDescriptor pd : pds) {
            System.out.println(pd.getName());
            //System.out.println(pd.getDisplayName());
            //System.out.println(pd.getShortDescription());
        }
	}
	
	/**
     * 获取传入JavaBean的所有属性，不包含其父类属性
     * 
     * @throws Exception
     */
    //@Test
    public void test2() throws Exception {
        //始停止分析其基类的JavaBean属性
        BeanInfo beanInfo = Introspector.getBeanInfo(Student.class, Object.class);
        
        //通过BeanInfo获取属性描述器数组
        PropertyDescriptor[] pds  = beanInfo.getPropertyDescriptors();
        
        //获取Student所有的属性(luckyNum, name), 不获取Object父类的属性
        for (PropertyDescriptor pd : pds) {
        	System.out.println("属性名称 -----"+pd.getName());
            System.out.println("属性类型 -----"+pd.getPropertyType().toString());
            System.out.println("属性的get方法 -----"+pd.getReadMethod().toGenericString());
            System.out.println("属性的set方法 -----"+pd.getWriteMethod().toGenericString());
        }
    }
    
    /**
     * 获取传入的JavaBean的指定属性
     * 
     * @throws Exception
     */
    //@Test
    public void test3() throws Exception {
        Student student = new Student();
        
        //获取指定的Bean属性(字段)
        PropertyDescriptor pd = new PropertyDescriptor("luckyNum", Student.class);
        //获取属性的真实类型
        System.out.println(pd.getPropertyType());           
         
        //获取属性的写方法，为属性赋值
        Method method = pd.getWriteMethod();
        //调用set方法
        method.invoke(student, 88);
        System.out.println(student.getLuckyNum());
         
        //调用读方法(getXX())
        Method method2 = pd.getReadMethod();
        System.out.println(method2.invoke(student));
    }
    
    /**
     * BeanUtils 简单演示
     * 
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    @Test
    public void test4() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    	Student student = new Student();
    	BeanUtils.setProperty(student, "luckyNum", "100");
    	String luckyNum = BeanUtils.getProperty(student, "luckyNum");
    	System.out.println(luckyNum);
    }
}
